package longcaisuyun.longcai.com.navi;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.enums.PathPlanningStrategy;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.autonavi.tbt.TrafficFacilityInfo;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.yiliubabalongcaisuyun.MyApplication;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.R;


public class BaseActivity extends Activity implements AMapNaviListener, AMapNaviViewListener {

    AMapNaviView mAMapNaviView;
    AMapNavi mAMapNavi;
    TTSController mTtsManager;
    NaviLatLng mEndLatlng ;
    NaviLatLng mStartLatlng ;
    NaviLatLng myLng;
    List<NaviLatLng> mStartList = new ArrayList<NaviLatLng>();
    List<NaviLatLng> mEndList = new ArrayList<NaviLatLng>();
    List<NaviLatLng> mWayPointList=new ArrayList<NaviLatLng>();
    String checkinfo;
    private LinearLayout fanhui_lin;

    private Dialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        View view=getWindow().getDecorView();
        MyApplication.ScaleScreenHelper.loadView((ViewGroup)view);
        //返回
        fanhui_lin= (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                finish();
                diag();
            }
        });
        //接值
        Intent intent=getIntent();
        String qijing = intent.getStringExtra("qijing");
        String qiwei = intent.getStringExtra("qiwei");
        String zhongjing = intent.getStringExtra("zhongjing");
        String zhongwei = intent.getStringExtra("zhongwei");
        checkinfo=intent.getStringExtra("checkinfo");
        //设置经纬度
        mStartLatlng=new NaviLatLng(Double.valueOf(qijing),Double.valueOf(qiwei));
        mEndLatlng=new NaviLatLng(Double.valueOf(zhongjing),Double.valueOf(zhongwei));
        //自己的位置
        String[] loc = MyApplication.myPreferences.readProvinceId().split(",");
        myLng=new NaviLatLng(Double.valueOf(loc[0]),Double.valueOf(loc[1])) ;


        mAMapNaviView = (AMapNaviView) findViewById(R.id.route_map);
        mAMapNaviView.onCreate(savedInstanceState);
        //为了尽最大可能避免内存泄露问题，建议这么写
        mTtsManager = TTSController.getInstance(getApplicationContext());
        mTtsManager.init();
        mTtsManager.startSpeaking();
        //为了尽最大可能避免内存泄露问题，建议这么写
        mAMapNavi = AMapNavi.getInstance(this);
        mAMapNavi.setAMapNaviListener(this);
        mAMapNavi.setAMapNaviListener(mTtsManager);
        mAMapNavi.setEmulatorNaviSpeed(150);

    }
    public void diag() {
        LayoutInflater inflater = LayoutInflater.from(BaseActivity.this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item_caozuo_phone, null);
        TextView tv_title = (TextView) layout.findViewById(R.id.textView28);
        tv_title.setText("提示：确认退出导航");
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog = new Dialog(BaseActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.setCancelable(false);
        dialog.show();
        dialog.setContentView(layout);
        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        tv_phone.setText("确定");
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        tv_phone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
           finish();
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                dialog.dismiss();

            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        mAMapNaviView.onResume();
        mStartList.add(myLng);
        mWayPointList.add(mStartLatlng);
        mEndList.add(mEndLatlng);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAMapNaviView.onPause();

        mTtsManager.stopSpeaking();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAMapNaviView.onDestroy();
        //������ִ��
        mAMapNavi.stopNavi();
        mAMapNavi.destroy();
        mTtsManager.destroy();
    }

    @Override
    public void onInitNaviFailure() {
//        Toast.makeText(this, "init navi Failed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onInitNaviSuccess() {
        if (checkinfo.equals("1")){
            mAMapNavi.calculateDriveRoute(mStartList, mWayPointList, null, PathPlanningStrategy.DRIVING_SHORT_DISTANCE);
        }else{
            mAMapNavi.calculateDriveRoute(mWayPointList, mEndList, null, PathPlanningStrategy.DRIVING_SHORT_DISTANCE);
        }
    }

    @Override
    public void onStartNavi(int type) {

    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    @Override
    public void onLocationChange(AMapNaviLocation location) {

    }

    @Override
    public void onGetNavigationText(int type, String text) {

    }

    @Override
    public void onEndEmulatorNavi() {
    }

    @Override
    public void onArriveDestination() {
    }

    @Override
    public void onCalculateRouteSuccess() {
        mAMapNavi.startNavi(AMapNavi.GPSNaviMode);
    }

    @Override
    public void onCalculateRouteFailure(int errorInfo) {
    }

    @Override
    public void onReCalculateRouteForYaw() {

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int wayID) {

    }

    @Override
    public void onGpsOpenStatus(boolean enabled) {
    }

    @Override
    public void onNaviSetting() {
    }

    @Override
    public void onNaviMapMode(int isLock) {

    }

    @Override
    public void onNaviCancel() {
        finish();
    }


    @Override
    public void onNaviTurnClick() {

    }

    @Override
    public void onNextRoadClick() {

    }


    @Override
    public void onScanViewButtonClick() {
    }

    @Deprecated
    @Override
    public void onNaviInfoUpdated(AMapNaviInfo naviInfo) {
    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviinfo) {
    }

    @Override
    public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

    }

    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {
    }

    @Override
    public void hideCross() {
    }

    @Override
    public void showLaneInfo(AMapLaneInfo[] laneInfos, byte[] laneBackgroundInfo, byte[] laneRecommendedInfo) {

    }

    @Override
    public void hideLaneInfo() {

    }

    @Override
    public void onCalculateMultipleRoutesSuccess(int[] ints) {

    }




    @Override
    public void onLockMap(boolean isLock) {
    }


    @Override
    public boolean onNaviBackClick() {
        return false;
    }


}
